home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / cc02.arc / DETAB.C < prev    next >
Text File  |  1986-03-14  |  2KB  |  73 lines

  1. /* ------------------------------------------- */
  2. /*     DETAB - Convert tabs to blanks          */
  3. /*             Adapted from Software Tools     */
  4. /*             By Kernighan and Plauger        */
  5. /*                                             */
  6. /*             written by Michael Burton       */
  7. /*             Last Update: 14 Jan 1984       */
  8. /* ------------------------------------------- */
  9. /*     USAGE:                                  */
  10. /*             DETAB FROMFILE TOFILE N         */
  11. /*               N is the number of columns    */
  12. /*                 between tab stops           */
  13. /* ------------------------------------------- */
  14. #include "stdio.h"
  15.  
  16. main(argc,argv)
  17.        int argc;
  18.        char *argv[];
  19. {
  20.        static int col = 1, n, *fd, *td;
  21.        static char c, *sp;
  22.  
  23.        if (argc != 4)
  24.        {
  25.                fputs("Usage: DETAB FROMFILE TOFILE N\007\n",stdout);
  26.                return;
  27.        }
  28.        sp = argv[1];
  29.        while ((*sp = toupper(*sp)) != EOS) sp++;
  30.        sp = argv[2];
  31.        while ((*sp = toupper(*sp)) != EOS) sp++;
  32.        if ((fd = fopen(argv[1],"r")) == 0)
  33.        {
  34.                fputs(argv[1],stdout);
  35.                fputs(" not found\007\n",stdout);
  36.                return;
  37.        }
  38.        if ((td = fopen(argv[2],"w")) == 0)
  39.        {
  40.                fputs("Unable to open ",stdout);
  41.                fputs(argv[2],stdout);
  42.                fputs("\007\n",stdout);
  43.                return;
  44.        }
  45.        n = atoi(argv[3]);
  46.        if (n < 1 || n > 32) fputs("Tabs < 1 or > 32\007\n",stdout);
  47.        while ((c = fgetc(fd)) != EOF)
  48.        {
  49.                switch (c)
  50.                {
  51.                        case '\t':
  52.                                do
  53.                                {
  54.                                        fputc(' ',td);
  55.                                        col++;
  56.                                }
  57.                                while ((col % n) != 0);
  58.                                break;
  59.                        case '\n':
  60.                                fputc('\n',td);
  61.                                col = 1;
  62.                                break;
  63.                        default:
  64.                                fputc(c,td);
  65.                                col++;
  66.                }
  67.        }
  68.        fflush(fd);
  69.        fflush(td);
  70.        fclose(fd);
  71.        fclose(td);
  72. }
  73.